package com.moxiao.yfjh.module.maker.service.makerProductInfo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.module.maker.controller.admin.makerProductInfo.vo.MatchmakerProductInfoCreateReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.makerProductInfo.vo.MatchmakerProductInfoExportReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.makerProductInfo.vo.MatchmakerProductInfoPageReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.makerProductInfo.vo.MatchmakerProductInfoUpdateReqVO;
import com.moxiao.yfjh.module.maker.convert.makerProductInfo.MatchmakerProductInfoConvert;
import com.moxiao.yfjh.module.maker.dal.dataobject.makerProductInfo.MatchmakerProductInfoDO;
import com.moxiao.yfjh.module.maker.dal.mysql.makerProductInfo.MatchmakerProductInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.maker.enums.ErrorCodeConstants.MATCHMAKER_PRODUCT_INFO_NOT_EXISTS;

/**
 * 相亲红娘商品信息 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class MatchmakerProductInfoServiceImpl implements MatchmakerProductInfoService {

    @Resource
    private MatchmakerProductInfoMapper matchmakerProductInfoMapper;

    @Override
    public Long createMatchmakerProductInfo(MatchmakerProductInfoCreateReqVO createReqVO) {
        // 插入
        MatchmakerProductInfoDO matchmakerProductInfo = MatchmakerProductInfoConvert.INSTANCE.convert(createReqVO);
        matchmakerProductInfoMapper.insert(matchmakerProductInfo);
        // 返回
        return matchmakerProductInfo.getId();
    }

    @Override
    public void updateMatchmakerProductInfo(MatchmakerProductInfoUpdateReqVO updateReqVO) {
        // 校验存在
        validateMatchmakerProductInfoExists(updateReqVO.getId());
        // 更新
        MatchmakerProductInfoDO updateObj = MatchmakerProductInfoConvert.INSTANCE.convert(updateReqVO);

        matchmakerProductInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteMatchmakerProductInfo(Long id) {
        // 校验存在
        validateMatchmakerProductInfoExists(id);
        // 删除
        matchmakerProductInfoMapper.deleteById(id);
    }

    private void validateMatchmakerProductInfoExists(Long id) {
        if (matchmakerProductInfoMapper.selectById(id) == null) {
            throw exception(MATCHMAKER_PRODUCT_INFO_NOT_EXISTS);
        }
    }

    @Override
    public MatchmakerProductInfoDO getMatchmakerProductInfo(Long id) {
        return matchmakerProductInfoMapper.selectById(id);
    }

    @Override
    public List<MatchmakerProductInfoDO> getMatchmakerProductInfoList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return matchmakerProductInfoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MatchmakerProductInfoDO> getMatchmakerProductInfoPage(MatchmakerProductInfoPageReqVO pageReqVO) {
        return matchmakerProductInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MatchmakerProductInfoDO> getMatchmakerProductInfoList(MatchmakerProductInfoExportReqVO exportReqVO) {
        return matchmakerProductInfoMapper.selectList(exportReqVO);
    }

    @Override
    public MatchmakerProductInfoDO getLastNo(Date todayStart, Date todayEnd) {
        LambdaQueryWrapper<MatchmakerProductInfoDO> lqw = Wrappers.lambdaQuery();
        if (Objects.nonNull(todayStart) && Objects.nonNull(todayEnd)) {
            lqw.between(MatchmakerProductInfoDO::getCreateTime, todayStart, todayEnd);
        }
        lqw.orderByDesc(MatchmakerProductInfoDO::getCreateTime);
        lqw.last("LIMIT " + 1);
        return matchmakerProductInfoMapper.selectOne(lqw);
    }

    @Override
    public Integer repeatChecking(String code) {
        LambdaQueryWrapper<MatchmakerProductInfoDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(true, MatchmakerProductInfoDO::getNo, code);
        List<MatchmakerProductInfoDO> list = matchmakerProductInfoMapper.selectList(lqw);
        return list.size();
    }

    @Override
    public Integer getMakerSalesVolume(Long productId) {
        return matchmakerProductInfoMapper.getMakerSalesVolume(productId);
    }


}
